﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AspNetMvc.Services.Events.Interfaces;
using CoreVirtuoso.Config;
using WebGuiMvc.Helpers;

namespace AspNetMvc.Services.Events
{
   internal class EventMinutesListService : IEventMinutesListService
   {
      private readonly IEventMinutesListGenerator _eventMinutesListGenerator;

      public EventMinutesListService(IEventMinutesListGenerator eventMinutesListGenerator)
      {
         _eventMinutesListGenerator = eventMinutesListGenerator;
      }

      public List<SelectListItem> GetMinutesList()
      {
         var minutesList = GetMinutesListWithDummyListItem();

         return minutesList;
      }

      public List<SelectListItem> GetMinutesList(int minuteToSelect)
      {
         var minutesList = GetMinutesListWithDummyListItem();

         //ToDo: Here we use minute2string conversation algorithm (in this case just ToString method call) outside of EventMinutesListGenerator
         var itemToSelect = minutesList.Find(sli => sli.Value == minuteToSelect.ToString());

         //ToDo: If user modified minutes value by tool like Firebug and the value is not multiple to five - item won't be selected?
         //  Should we take care of it during event creation validation?
         if (itemToSelect != null)
            itemToSelect.Selected = true;

         return minutesList;
      }

      private List<SelectListItem> GetMinutesListWithDummyListItem()
      {
         var minutesList = _eventMinutesListGenerator.GetMinutesList();

         var dummyListItem = new SelectListItem() { Value = DummyItemsConfig.DummyItemValue, Text = ViewStrings.EventsStrings.Minutes };
         minutesList.Insert(0, dummyListItem);

         return minutesList;
      }
   }
}